'use strict';

// Usuarios
angular.module('simon.controllers').controller('UsuarioCtrl', ['$scope', 'UsuarioFactory', 'UsuarioIdFactory', 'FuncionarioFactory', 'AlertFactory', 'ModalFactory',
  function ($scope, UsuarioFactory, UsuarioIdFactory, FuncionarioFactory, AlertFactory, ModalFactory) {
	/* callback for ng-click 'create': */
    $scope.create = function () {
		$scope.isNew = true;
		$scope.item = {funcionario: '', login: '', senha: '', nivel: '', ativo: 'S'};
    };
	
	/* callback for ng-click 'edit': */
    $scope.edit = function (item) {
		$scope.isNew = false;
		$scope.item = item;
    };
    
    /* callback for ng-click 'insert': */
    $scope.insert = function () {
    	$scope.item = UsuarioFactory.create($scope.item, 
    		function (success) { 
    			$scope.itens.push($scope.item); $scope.$broadcast("RESET_FORM"); AlertFactory.add("success", "Registro salvo com sucesso!");
    		},
    		function (error) { AlertFactory.add("danger", "Falha ao inserir o registro!"); }
    	);
    };
	
	/* callback for ng-click 'update': */
    $scope.update = function () {
		$scope.item = UsuarioIdFactory.update($scope.item, 
			function (success) { $scope.$broadcast("RESET_FORM"); AlertFactory.add("success", "Registro salvo com sucesso!"); },
			function (error) { AlertFactory.add("danger", "Falha ao atualizar o registro!"); }
		);
    };
    
    /* callback for ng-click 'remove': */
    $scope.remove = function (rowId, idx) {
    	var modal = ModalFactory.confirm();
    	modal.result.then(function () {
			UsuarioIdFactory.remove({ id: rowId }, 
				function (success) { $scope.itens.splice(idx, 1); AlertFactory.add("success", "Registro removido com sucesso!"); },
	    		function (error) { AlertFactory.add("danger", "Falha ao excluir o registro!"); }
			);
	    });
    };

    $scope.itens = UsuarioFactory.query();
    $scope.funcionarios = FuncionarioFactory.query();
  }]);

angular.module('simon.controllers').filter('nivelFilter', function() {
    return function(input) {
        var out = "Administrador";
        if (input == "2") {
        	out = "Gerente";
        } else if (input == "3") {
        	out = "Caixa";
        }
        return out;
    };
});
